package com.wangdao.wechatmall.service.admin;

import com.github.pagehelper.PageInfo;
import com.wangdao.wechatmall.bean.bo.admin.CollectListBO;
import com.wangdao.wechatmall.bean.bo.wx.WXAddordeleteBO;
import com.wangdao.wechatmall.bean.bo.wx.WXCollectListBO;
import com.wangdao.wechatmall.bean.pojo.*;
import com.wangdao.wechatmall.bean.vo.admin.BaseDataVO;
import com.wangdao.wechatmall.bean.vo.wx.CollectListVO;
import com.wangdao.wechatmall.mapper.*;
import com.wangdao.wechatmall.util.MyPageHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

/**
 * @author ZhangXiao on 2021/7/8
 */
@Service
public class CollectServiceImpl implements CollectService {

    @Autowired
    CollectMapper collectMapper;

    @Autowired
    FootprintMapper footprintMapper;

    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    TopicMapper topicMapper;

    @Autowired
    UserMapper userMapper;

    @Override
    public BaseDataVO<Collect> queryCollect(CollectListBO collectListBO) {
        MyPageHelper.startPage(collectListBO);

        CollectExample collectExample = new CollectExample();
        CollectExample.Criteria criteria = collectExample.createCriteria();

        Integer userId = collectListBO.getUserId();
        Integer valueId = collectListBO.getValueId();

        if (userId != null) {
            criteria.andUserIdEqualTo(userId);
        }

        if (valueId != null) {
            criteria.andUserIdEqualTo(valueId);
        }

        collectExample.setOrderByClause(collectListBO.getSort() + " " + collectListBO.getOrder());

        List<Collect> collects = collectMapper.selectByExample(collectExample);

        PageInfo<Collect> collectPageInfo = new PageInfo<>(collects);
        long total = collectPageInfo.getTotal();


        return BaseDataVO.ok(collects, total);
    }

    @Override
    public CollectListVO selectCollectList(WXCollectListBO collectListBO) {
        MyPageHelper.startPage(collectListBO);
        CollectListVO collectListVO = new CollectListVO();

        List<CollectListVO.CollectListBean> collectListBeans = new ArrayList<>();


        //先查出需要的collect中的信息
        List<Collect> collects = collectMapper.queryCollectList();
        for (Collect collect : collects) {
            Byte type = collect.getType();
            CollectListVO.CollectListBean collectListBean = new CollectListVO.CollectListBean();
            if (type == 0) {
                //当为0时，valueId为goodsId
                Integer goodsId = collect.getValueId();
                Goods goods = goodsMapper.selectGoods(goodsId);
                if (collect.getDeleted() == false) {
                    collectListBean.setType(type);
                    collectListBean.setId(collect.getId());
                    collectListBean.setValueId(collect.getValueId());
                    collectListBean.setBrief(goods.getBrief());
                    collectListBean.setName(goods.getName());
                    collectListBean.setPicUrl(goods.getPicUrl());
                    collectListBean.setRetailPrice(goods.getRetailPrice());
                    collectListBeans.add(collectListBean);
                }
            }
            if (type == 1) {
                //当为1时，valueId为专题id（即需要到topic表中去查询）
                Integer valueId = collect.getValueId();
                Topic topic = topicMapper.selectTopic(valueId);
                Integer[] goods = topic.getGoods();
                Integer goodsId = goods[0];
                Goods goods1 = goodsMapper.selectGoods(goodsId);
                collectListBean.setType(type);
                collectListBean.setId(collect.getId());
                collectListBean.setValueId(collect.getValueId());
                collectListBean.setBrief(goods1.getBrief());
                collectListBean.setName(goods1.getName());
                collectListBean.setPicUrl(goods1.getPicUrl());
                collectListBean.setRetailPrice(goods1.getRetailPrice());
                collectListBeans.add(collectListBean);

            }

        }
        PageInfo<Collect> collectPageInfo = new PageInfo<>();
        long total = collectPageInfo.getTotal();
        collectListVO.setTotalPages((int) total);
        collectListVO.setCollectList(collectListBeans);
        return collectListVO;
    }

    @Override
    public int addCollect(WXAddordeleteBO wxAddordeleteBO) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.getPrincipals() != null && subject.getPrincipals().getPrimaryPrincipal() != null) {
            String username = (String) subject.getPrincipals().getPrimaryPrincipal();
            Integer valueId = wxAddordeleteBO.getValueId();
            int userId = userMapper.queryId(username);

            //根据请求参数来判断表中是否有对应的数据
            CollectExample collectExample = new CollectExample();
            CollectExample.Criteria criteria = collectExample.createCriteria();
            criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false).andValueIdEqualTo(valueId);
            List<Collect> collects = collectMapper.selectByExample(collectExample);
            if (collects.size() != 0) {
                //当不为null，说明表中有相同物品的收藏，需要逻辑删除
                int temp = 1;
                collectMapper.updateDelete(valueId, temp);
                return 2;
            } else {
                Collect collect = new Collect();
                collect.setUserId(userId);

                Byte type = wxAddordeleteBO.getType();
                collect.setType(wxAddordeleteBO.getType());
                collect.setValueId(wxAddordeleteBO.getValueId());
                Date date = new Date();
                collect.setAddTime(date);
                collect.setAddTime(date);
                collect.setDeleted(false);
                collectMapper.insertSelective(collect);
                return 1;
            }
        }
        return 0;

    }

}



